$(function(argument) {
	var region_id;
	var region_name;
	var main_id;
	var lng1;
	var lat1;
	region_id = window.sessionStorage.getItem("region_id");
	region_name = window.sessionStorage.getItem("region_name");
	if (region_id == null) {
		region_id = 52;
		$(".currentCity").html("北京");
	} else {
		$(".currentCity").html(region_name);
		// window.sessionStorage.removeItem("region_name");
	}
	$.ajax({
		url: 'http://www.teaweilai.com/app/Admin/City/CatShow',
		type: 'POST',
		dataType: 'json',
		success: function(data) {
			console.log(data);
			var ht = template("nav-tabs", {
				mall: data.sing
			});
			$(".nav-tabs").html(ht);
			goods_detail(2)
			$('.nav-tabs li a').on('click', function() {
				var mall_id = $(this).attr('data_mall_id')
				goods_detail(mall_id);
			})
		}
	})

	// 获取定位方法
	function getLcation(callback) {
		var map = new BMap.Map("allmap");
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r) {
			if (this.getStatus() == BMAP_STATUS_SUCCESS) {
				var mk = new BMap.Marker(r.point);
				map.addOverlay(mk);
				map.panTo(r.point);
				lng1 = r.point.lng;
				lat1 = r.point.lat;
				callback();
				// alert('您的位置：' + r.point.lng + ',' + r.point.lat);
			} else {
				alert('failed' + this.getStatus());
			}
		}, {
			enableHighAccuracy: true
		})

	}

	function goods_detail(mall_id) {


		getLcation(function() {
			$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/City/EachMeter',
				type: 'POST',
				dataType: 'json',
				data: {
					region_id: region_id,
					lng1: lng1,
					lat1: lat1,
					mall_id: mall_id
				},
				success: function(data) {
					console.log(data)
					if (data.code == 200) {
						var ht = template("cityShop", {
							mall: data
						});
						$(".cityShop").html(ht);
					} else {
						$(".cityShop").html('<h2 style="text-align: center;">等待商家入驻</h2>');
					}
				}
			})
		})


	}

	// tab滑动切换
	function my_tab(dom, patrens, changeLi) {
		//定义变量
		//是否移动
		var isMove = false;
		//定义手指点击到离开的时间
		var delayTime = 100;
		//记录开始时间
		var startTime = 0;
		// 记录开始位置
		var startSite = 0;
		// 记录结束位置
		var endSite = 0;
		//可移动的tab标签范围
		var tabLength = 0;
		$(dom).on('touchstart', function(e) {
			// 将 isMove 设置为初始值 false
			isMove = false;
			//获取当前时间
			startTime = Date.now();
			startSite = e.changedTouches[0].clientX;
		})
		$(dom).on('touchmove', function() {
			isMove = true;
		})
		$(dom).on('touchend', function(e) {
			if (isMove) {
				endSite = e.changedTouches[0].clientX;
				console.log(startSite);
				console.log(endSite);
				if (startSite > endSite) {
					// 滑向下一个
					console.log("next");
					if ($(changeLi).hasClass('active')) {
						tabLength += 1;
						if (tabLength >= $(changeLi).length) {
							tabLength == $(changeLi).length
						}
						$(changeLi).removeClass('active');
						$(changeLi).eq(tabLength).addClass('active');
						var mall_id = $(changeLi).eq(tabLength).find("a").attr("data_mall_id");
						console.log(mall_id);
						goods_detail(mall_id);
						console.log(tabLength);

					}
				} else if (startSite < endSite) {
					// 滑向上一个
					console.log("prev");
					if ($(changeLi).hasClass('active')) {
						tabLength -= 1;
						if (tabLength <= 0) {
							tabLength == 0;
						}
						$(changeLi).removeClass('active');
						$(changeLi).eq(tabLength).addClass('active');
						var mall_id = $(changeLi).eq(tabLength).find("a").attr("data_mall_id");
						console.log(mall_id);
						goods_detail(mall_id);
						console.log(tabLength);
					}
				}
			} else if ((Date.now() - startTime) > delayTime) {
				return;
			}
		})
	}
	my_tab(".tab-content", ".container", ".nav-tabs li");


	template.helper("meter", function(a) {
			var meter = (Number(a) / 1000).toFixed(2);
			return meter;
		})
		// 点击城市
	$(".currentCity").click(function() {
		window.location.href = "selectCity.html";
		window.sessionStorage.setItem("backCity", 1);
	})

	// 点击进入商家详情
	$(document).on("click", ".shops", function() {
		var sid = $(this).eq(0).attr('data-seller_id');
		window.sessionStorage.setItem("sid", sid);
		window.location.href = "waterBusinessperson.html";
	})


	// 点击返回
	$(".titleTea a").click(function(e) {
		e.preventDefault();
		window.location.href = "../index.html";
		window.sessionStorage.removeItem("region_id");
		window.sessionStorage.removeItem("region_name");
	})
})